package cn.iocoder.yudao.module.eval.controller.admin.candidate;

import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;
import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import cn.iocoder.yudao.module.eval.controller.admin.candidate.vo.*;
import cn.iocoder.yudao.module.eval.dal.dataobject.candidate.EvalCandidateInfoDO;
import cn.iocoder.yudao.module.eval.service.candidate.EvalCandidateService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.util.Collection;
import java.util.List;

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;
import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.*;

@Tag(name = "管理后台 - 参评人员管理")
@RestController
@RequestMapping("/eval/candidate")
@Validated
@Slf4j
public class EvalCandidateController {

    @Resource
    private EvalCandidateService candidateService;

    @PostMapping("/create")
    @Operation(summary = "创建参评人员")
    @PreAuthorize("@ss.hasPermission('eval:candidate:create')")
    @ApiAccessLog(operateType = CREATE)
    public CommonResult<Long> createCandidate(@Valid @RequestBody EvalCandidateInfoSaveReqVO createReqVO) {
        return success(candidateService.createCandidate(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新参评人员")
    @PreAuthorize("@ss.hasPermission('eval:candidate:update')")
    @ApiAccessLog(operateType = UPDATE)
    public CommonResult<Boolean> updateCandidate(@Valid @RequestBody EvalCandidateInfoSaveReqVO updateReqVO) {
        candidateService.updateCandidate(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除参评人员")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('eval:candidate:delete')")
    @ApiAccessLog(operateType = DELETE)
    public CommonResult<Boolean> deleteCandidate(@RequestParam("id") Long id) {
        candidateService.deleteCandidate(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得参评人员")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('eval:candidate:query')")
    public CommonResult<EvalCandidateInfoRespVO> getCandidate(@RequestParam("id") Long id) {
        EvalCandidateInfoDO candidate = candidateService.getCandidate(id);
        return success(BeanUtils.toBean(candidate, EvalCandidateInfoRespVO.class));
    }

    @GetMapping("/list")
    @Operation(summary = "获得参评人员列表")
    @Parameter(name = "ids", description = "编号列表", required = true, example = "1024,2048")
    @PreAuthorize("@ss.hasPermission('eval:candidate:query')")
    public CommonResult<List<EvalCandidateInfoRespVO>> getCandidateList(@RequestParam("ids") Collection<Long> ids) {
        List<EvalCandidateInfoDO> list = candidateService.getCandidateList(ids);
        return success(BeanUtils.toBean(list, EvalCandidateInfoRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得参评人员分页")
    @PreAuthorize("@ss.hasPermission('eval:candidate:query')")
    public CommonResult<PageResult<EvalCandidateInfoRespVO>> getCandidatePage(@Valid EvalCandidateInfoPageReqVO pageReqVO) {
        PageResult<EvalCandidateInfoDO> pageResult = candidateService.getCandidatePage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, EvalCandidateInfoRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出参评人员 Excel")
    @PreAuthorize("@ss.hasPermission('eval:candidate:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportCandidateExcel(@Valid EvalCandidateInfoPageReqVO pageReqVO,
                                   HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<EvalCandidateInfoDO> list = candidateService.getCandidatePage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "参评人员.xls", "数据", EvalCandidateInfoRespVO.class,
                BeanUtils.toBean(list, EvalCandidateInfoRespVO.class));
    }

    @PostMapping("/import-excel")
    @Operation(summary = "批量导入参评人员")
    @Parameter(name = "file", description = "Excel 文件", required = true)
    @Parameter(name = "planId", description = "评价计划ID", required = true)
    @PreAuthorize("@ss.hasPermission('eval:candidate:import')")
    @ApiAccessLog(operateType = IMPORT)
    public CommonResult<Object> importCandidateExcel(@RequestParam("file") MultipartFile file,
                                                     @RequestParam("planId") Long planId) throws Exception {
        List<EvalCandidateInfoSaveReqVO> list = ExcelUtils.read(file, EvalCandidateInfoSaveReqVO.class);
        return success(candidateService.importCandidates(planId, list));
    }

    @GetMapping("/import-template")
    @Operation(summary = "获得导入参评人员模板")
    @PreAuthorize("@ss.hasPermission('eval:candidate:import')")
    public void importCandidateTemplate(HttpServletResponse response) throws IOException {
        // 导出 Excel
        ExcelUtils.write(response, "参评人员导入模板.xls", "模板", EvalCandidateInfoSaveReqVO.class,
                List.of());
    }

    @PostMapping("/batch-add")
    @Operation(summary = "批量添加参评人员到计划")
    @PreAuthorize("@ss.hasPermission('eval:candidate:create')")
    @ApiAccessLog(operateType = CREATE)
    public CommonResult<Boolean> batchAddCandidatesToPlan(@Valid @RequestBody EvalCandidateBatchAddReqVO batchAddReqVO) {
        candidateService.batchAddCandidatesToPlan(batchAddReqVO.getPlanId(), batchAddReqVO.getCandidateIds());
        return success(true);
    }

    @PostMapping("/batch-remove")
    @Operation(summary = "批量从计划中移除参评人员")
    @PreAuthorize("@ss.hasPermission('eval:candidate:delete')")
    @ApiAccessLog(operateType = DELETE)
    public CommonResult<Boolean> batchRemoveCandidatesFromPlan(@Valid @RequestBody EvalCandidateBatchRemoveReqVO batchRemoveReqVO) {
        candidateService.batchRemoveCandidatesFromPlan(batchRemoveReqVO.getPlanId(), batchRemoveReqVO.getCandidateIds());
        return success(true);
    }

    @GetMapping("/plan/{planId}")
    @Operation(summary = "获取计划的参评人员列表")
    @Parameter(name = "planId", description = "计划ID", required = true)
    @PreAuthorize("@ss.hasPermission('eval:candidate:query')")
    public CommonResult<List<EvalCandidateInfoRespVO>> getCandidatesByPlan(@PathVariable("planId") Long planId) {
        List<EvalCandidateInfoDO> list = candidateService.getCandidatesByPlan(planId);
        return success(BeanUtils.toBean(list, EvalCandidateInfoRespVO.class));
    }

    @GetMapping("/plan/{planId}/page")
    @Operation(summary = "分页获取计划的参评人员")
    @Parameter(name = "planId", description = "计划ID", required = true)
    @PreAuthorize("@ss.hasPermission('eval:candidate:query')")
    public CommonResult<PageResult<EvalCandidateInfoRespVO>> getCandidatePageByPlan(@PathVariable("planId") Long planId,
                                                                                   @Valid EvalCandidateInfoPageReqVO pageReqVO) {
        pageReqVO.setPlanId(planId);
        PageResult<EvalCandidateInfoDO> pageResult = candidateService.getCandidatePage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, EvalCandidateInfoRespVO.class));
    }

    @GetMapping("/statistics")
    @Operation(summary = "参评人员统计")
    @Parameter(name = "planId", description = "计划ID", required = false)
    @PreAuthorize("@ss.hasPermission('eval:candidate:query')")
    public CommonResult<Object> getCandidateStatistics(@RequestParam(value = "planId", required = false) Long planId) {
        return success(candidateService.getCandidateStatistics(planId));
    }

    @PostMapping("/qualification-review")
    @Operation(summary = "资格审核")
    @PreAuthorize("@ss.hasPermission('eval:candidate:review')")
    @ApiAccessLog(operateType = UPDATE)
    public CommonResult<Boolean> qualificationReview(@Valid @RequestBody EvalCandidateQualificationReviewReqVO reviewReqVO) {
        candidateService.qualificationReview(reviewReqVO);
        return success(true);
    }

    @PostMapping("/batch-qualification-review")
    @Operation(summary = "批量资格审核")
    @PreAuthorize("@ss.hasPermission('eval:candidate:review')")
    @ApiAccessLog(operateType = UPDATE)
    public CommonResult<Boolean> batchQualificationReview(@Valid @RequestBody EvalCandidateBatchQualificationReviewReqVO batchReviewReqVO) {
        candidateService.batchQualificationReview(batchReviewReqVO);
        return success(true);
    }

    @GetMapping("/qualification-review/pending")
    @Operation(summary = "获取待审核参评人员")
    @Parameter(name = "planId", description = "计划ID", required = false)
    @PreAuthorize("@ss.hasPermission('eval:candidate:query')")
    public CommonResult<PageResult<EvalCandidateInfoRespVO>> getPendingQualificationReview(@RequestParam(value = "planId", required = false) Long planId,
                                                                                          @Valid EvalCandidateInfoPageReqVO pageReqVO) {
        PageResult<EvalCandidateInfoDO> pageResult = candidateService.getPendingQualificationReview(planId, pageReqVO);
        return success(BeanUtils.toBean(pageResult, EvalCandidateInfoRespVO.class));
    }

    @GetMapping("/qualification-review/statistics")
    @Operation(summary = "资格审核统计")
    @Parameter(name = "planId", description = "计划ID", required = true)
    @PreAuthorize("@ss.hasPermission('eval:candidate:query')")
    public CommonResult<Object> getQualificationReviewStatistics(@RequestParam("planId") Long planId) {
        return success(candidateService.getQualificationReviewStatistics(planId));
    }

    @PostMapping("/update-registration-status")
    @Operation(summary = "更新报名状态")
    @PreAuthorize("@ss.hasPermission('eval:candidate:update')")
    @ApiAccessLog(operateType = UPDATE)
    public CommonResult<Boolean> updateRegistrationStatus(@Valid @RequestBody EvalCandidateRegistrationStatusUpdateReqVO updateReqVO) {
        candidateService.updateRegistrationStatus(updateReqVO.getId(), updateReqVO.getRegistrationStatus());
        return success(true);
    }

    @GetMapping("/search")
    @Operation(summary = "搜索参评人员")
    @Parameter(name = "keyword", description = "搜索关键词", required = true)
    @Parameter(name = "planId", description = "计划ID", required = false)
    @PreAuthorize("@ss.hasPermission('eval:candidate:query')")
    public CommonResult<List<EvalCandidateInfoRespVO>> searchCandidates(@RequestParam("keyword") String keyword,
                                                                       @RequestParam(value = "planId", required = false) Long planId) {
        List<EvalCandidateInfoDO> list = candidateService.searchCandidates(keyword, planId);
        return success(BeanUtils.toBean(list, EvalCandidateInfoRespVO.class));
    }

    @GetMapping("/list-by-audit-status")
    @Operation(summary = "根据审核状态获得参评人员列表")
    @Parameter(name = "auditStatus", description = "审核状态", required = true)
    @PreAuthorize("@ss.hasPermission('eval:candidate:query')")
    public CommonResult<List<EvalCandidateInfoRespVO>> getCandidateListByAuditStatus(@RequestParam("auditStatus") Integer auditStatus) {
        List<EvalCandidateInfoDO> list = candidateService.getCandidateListByAuditStatus(auditStatus);
        return success(BeanUtils.toBean(list, EvalCandidateInfoRespVO.class));
    }

}
